package com.project.gjbus.util;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@Data
public class RestResult<T> implements Serializable {

    private boolean success;

    private int code;

    private String msg;

    private T data;

    private RestResult() {
    }

    public RestResult(boolean success, String msg, T data) {
        this(success, 0, msg, data);
    }

    public RestResult(boolean success, int code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <R> RestResult<R> ok(String msg, R data) {
        return new RestResult<>(true, msg == null ? "OK" : msg, data);
    }

    public static <R> RestResult<R> ok(String msg) {
        return new RestResult<>(true, msg, null);
    }

    public static <R> RestResult<R> ok() {
        return RestResult.ok("OK", null);
    }

    public static <R> RestResult<R> ok(R data) {
        return RestResult.ok("OK", data);
    }

    public static <R> RestResult<R> error(HttpStatus httpStatus, String message, R data) {
        return new RestResult<>(false, httpStatus.value(), StringUtils.defaultString(message, "ERROR"), data);
    }

    public static <R> RestResult<R> error(int code, String msg, R data) {
        return new RestResult<>(false, code, msg == null ? "ERROR" : msg, data);
    }

    public static <R> RestResult<R> error(String msg, R data) {
        return RestResult.error(10000, msg, data);
    }

    public static <R> RestResult<R> error(String msg) {
        return RestResult.error(10000, msg, null);
    }

    public static <R> RestResult<R> error() {
        return RestResult.error(10000, "ERROR", null);
    }

}

